import { DataSource } from "typeorm";
import config from "./index";

export const AppDataSource = new DataSource({
  type: "mysql",
  host: config.db.host,
  port: config.db.port,
  username: config.db.username,
  password: config.db.password,
  database: config.db.database,
  entities: ["src/models/**/*.ts"],
  synchronize: false, // 开发模式不自动同步表结构
  logging: true,
  logger: "advanced-console",
});

export const connectDB = async () => {
  try {
    await AppDataSource.initialize();
    console.log("✅ MySQL 数据库连接成功");
  } catch (err) {
    console.error("❌ MySQL 数据库连接失败", err);
  }
};
